﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net.Mail;
using System.Xml.Serialization;
/// <summary>
/// Summary description for KorisnickiUtility
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]

public class KorisnickiUtility : System.Web.Services.WebService {

    public class DrvoPodaci
    {
        public string clanak
        {
            get;
            set;
        }

        public string id
        {
            get;
            set;
        }

     

        public DrvoPodaci(string clanak,string id)
        {
            this.clanak = clanak;
            this.id = id;
        }
    }

    public struct DrvoThumbs
    {
        public string nazivDrveta;
        public string linkThumb;
    }

    public KorisnickiUtility () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public void SaljiMail(string adresaPosiljaoca,string sadrzaj,string naslov ) {
        MailMessage m = new MailMessage(new MailAddress( adresaPosiljaoca),new MailAddress("maziljak@gmail.com"));
        m.Subject = naslov;
        m.Body = sadrzaj+ Environment.NewLine+"Posiljaoc: " + adresaPosiljaoca;
        SmtpClient client = new SmtpClient("smtp.gmail.com");
        client.Credentials = new System.Net.NetworkCredential("*****", "******");
        client.EnableSsl = true;
        client.Send (m);
        
    }
    [WebMethod]
    [XmlInclude(typeof(DrvoPodaci))]
    public List<DrvoPodaci> DrveceClanci(string naziv, int maksimalnaVisina,int rod, int minimalnaTemperatura)
    {
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 e = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
        var drv = from dr in e.Drvo
                 where (dr.HrvatskiNaziv.Contains(naziv)
                 || dr.LatinskiNaziv.Contains(naziv)) && (dr.MaksimalnaVisina <= maksimalnaVisina) &&(dr.MinimalnaTemperatura<=minimalnaTemperatura)
                 select dr;

        ;
        if (rod > -1)
        {
            drv = drv.Where(drvo => drvo.RodID == rod);
        }



        List<DrvoPodaci> podaci = new List<DrvoPodaci>();
        foreach (EnciklopedijaDrvecaModel.Drvo drrv in drv.ToList())
        {
            string[] arr = drrv.Clanak.Split(new string[] { "<br id='delimiter' />","<br id=\"delimiter\" />" }, StringSplitOptions.None);
            podaci.Add(new DrvoPodaci(arr[0],drrv.DrvoID.ToString()));
        }
        return podaci;
    }

    /// <summary>
    /// Vraca link na veliku sliku koji odgovara stazi za thumbnail
    /// Koristi se za pregled velike slike vezane uz thumbnail na odgovarajucem clanku 
    /// </summary>
    /// <param name="linkNaThumbnail"></param>
    /// <returns></returns>
     [WebMethod]
     public String LinkNaVelikuSliku(string linkNaThumbnail)
    {
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 e = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
        int i = linkNaThumbnail.IndexOf("Slike");
        string sub = linkNaThumbnail.Substring(i);
        var link = from l in e.Thumbnail
                   where l.Link == linkNaThumbnail.Substring(i)
                   select l.LinkNaVelikuSliku;
        if (link.Count() != 0)
            return link.First().ToString();
        else
            return null;
    }


    [WebMethod]
    public String OpisUzSliku(string linkNaThumbnail)
     {
         EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 e = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
         int i = linkNaThumbnail.IndexOf("Slike");
         var link = from l in e.Thumbnail
                    where l.Link == linkNaThumbnail.Substring(i)
                    select l.DodatniHtml;
         if (link.Count() != 0)
             return link.First().ToString();
         else
             return "";
     }


    /// <summary>
    /// Vraca 6 najnovije dodanih clanaka o drvecu
    /// </summary>
    /// <returns></returns>
    [WebMethod]
    public List<DrvoThumbs> NajnovijiClanciDrveceThumbs()
    {
        List<DrvoThumbs> list = new List<DrvoThumbs>();
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 e = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
        var drva = from drvo in e.Drvo
                   orderby drvo.DrvoID descending
                   select drvo.HrvatskiNaziv;
        drva = drva.Take(6);

        List<string> drvece = drva.ToList();
        foreach (string str in drvece)
        {
            var thmb = from th in e.Thumbnail
                       join dr in e.Drvo
                       on th.GalerijaID equals dr.GalerijaID
                       where dr.HrvatskiNaziv == str
                       select th.Link;
            string s = thmb.ToList<string>()[0];
            DrvoThumbs dt = new DrvoThumbs();
            dt.linkThumb = s;
            dt.nazivDrveta = str;
            list.Add(dt);
        }
        return list;
        
       
    }

    [WebMethod]
    public int DrvoID(string hrvatskiNaziv)
    {
        EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1 e = new EnciklopedijaDrvecaModel.EnciklopedijaDrvecaEntities1();
        var num = from n in e.Drvo
                  where n.HrvatskiNaziv == hrvatskiNaziv
                  select n.DrvoID;
        return num.First();
    }

}
